/**
 * 全局工具类
 * this.$kit.方法名（参数）
 * 举例：
 * <div>{{ $kit.helloWord("123") }}</div>
 * <div @click="$kit.helloWord('123')"></di
 */

/**
 * 获取数组的中心坐标（是中心，不是重心）
 * @param paths  [{x:1,y:1}]
 * @returns {{centerY: *, centerX: *}}
 */
let centerPosition = (paths) => {
    let minX = {x: paths[0].x, y: paths[0].y}
    let maxX = {x: paths[0].x, y: paths[0].y}
    let minY = {x: paths[0].x, y: paths[0].y}
    let maxY = {x: paths[0].x, y: paths[0].y}
    for (let i = 0; i < paths.length; i++) {
        let pX = paths[i].x
        let pY = paths[i].y

        if (pX <= minX.x) {
            minX.x = pX
            minX.y = pY
        }

        if (pX >= maxX.x) {
            maxX.x = pX
            maxX.y = pY
        }

        if (pY <= minY.y) {
            minY.x = pX
            minY.y = pY
        }
        if (pY >= maxY.y) {
            maxY.x = pX
            maxY.y = pY
        }
    }
    let centerX = (maxX.x - minX.x) / 2 + minX.x
    let centerY = (maxY.y - minY.y) / 2 + minY.y
    return {centerX: centerX, centerY: centerY}
}

let helloWord = (obj) => {
    ////////////console.log("helloWord:" + obj)
    return obj
}

let gen = (allList) => {
    let ids = [];
    let gen = [];
    for (let key in allList) {
        ids.push(allList[key]['id'] + "")
    }
    for (let key in allList) {
        let pid = allList[key]['pid'] + ""
        //pid在id里面不存在，说明它是根
        if (ids.indexOf(pid) === -1) {
            gen.push(allList[key])
        }
    }
    return gen
}

let sort = (parent_obj, allList) => {
    let child_list = [];
    for (let i = 0; i < allList.length; i++) {
        let now_obj = allList[i];
        let now_pid = now_obj["pid"];
        //    查得到
        if (now_pid.toString() == (parent_obj['id'] + "")) {
            child_list.push(now_obj);//同级别添加
            sort(now_obj, allList);//继续递归
        }
        //不同级别不做处理
        else {
        }
        if (child_list.length != 0) {
            parent_obj['children'] = child_list;
        }
    }
    return parent_obj
}

let getTree = (allList) => {
    //根数组
    let genArray = gen(allList);
    let genIds = genArray.map((e) => {
        return e['id']
    });

    //非根节点数组
    let childArray = [];
    for (let key in allList) {
        let id = allList[key]['id'] + ""
        //id不在根里，说明它是子路由
        if (genIds.indexOf(id) === -1) {
            childArray.push(allList[key])
        }
    }

    //非根节点数组
    let routerArray = [];
    for (let key in genArray) {
        routerArray.push(sort(genArray[key], childArray));
    }
    return routerArray
}

let getTreeIds = (allList, id = 'id', children = 'children', ids = []) => {

    for (let key in allList) {

        // 存一下ID
        if (allList[key][id]) {
            ids.push(allList[key][id])
        }

        //如果有children，继续循环
        if (allList[key] [children]) {
            ////////////console.log("如果有children，继续循环")
            getTreeIds(allList[key] [children], id, children, ids)
        }
    }
    return ids;
}


//删除首尾字符
let delStartEnd = (text, startChar, endChar) => {
    // var reg1 = /(^-*|-*$)/g;
    let reg2 = new RegExp('(^' + startChar + '*|' + endChar + '*$)', 'g');
    return text.replace(reg2, '')
}

// 横杠转驼峰
let barToHump = (text) => {
    let str = text.replace(/\-(\w)/g, function (all, letter) {
        return letter.toUpperCase();
    });
    return delStartEnd(str, '-', '-')
}

// 驼峰转横杠
let humpToBar = (text) => {
    let str = text.replace(/([A-Z])/g, "-$1").toLowerCase();
    return delStartEnd(str, '-', '-')
}

//下划线转驼峰
let _toHump = (text) => {
    let str = text.replace(/\_(\w)/g, function (all, letter) {
        return letter.toUpperCase();
    });
    return delStartEnd(str, '_', '_')
}

//驼峰转下划线
let humpTo_ = (text) => {
    let str = text.replace(/([A-Z])/g, "_$1").toLowerCase();
    return delStartEnd(str, '_', '_')
}

//打开
let open = (url, target, features, replace) => {
    window.open(url)
}


export default {
    helloWord: helloWord,
    getTree: getTree,
    getTreeIds: getTreeIds,
    delStartEnd: delStartEnd,
    barToHump: barToHump,
    humpToBar: humpToBar,
    _toHump: _toHump,
    humpTo_: humpTo_,
    open: open,
    centerPosition: centerPosition,
}
